'use strict';

describe('Service: queue', function () {

  // load the service's module
  beforeEach(module('webappApp'));

  // instantiate service
  var queue;
  beforeEach(inject(function(_queue_) {
    queue = _queue_;
  }));

  it('should execute pushed tasks when empty', function () {
    var executed = false,
    task = function () {
      executed = true;
    };
    queue.push(task);
    expect(executed).toBeTruthy();
  });

  it('shouldn\'t execute pushed tasks while at capacity', function () {
    var first = false, second = false;
    queue.capacity = 1;
    queue.push(function() {
      first = true;
    });
    queue.push(function() {
      second = true;
    });
    expect(first).toBeTruthy();
  });

  it('should remove a task when popped', function () {
    var task = function () {
      expect(queue.status.executing).toBe(1);
      expect(queue.status.queued).toBe(0);
      queue.pop();
    };
    queue.push(task);
    expect(queue.status.executing).toBe(0);
    expect(queue.status.queued).toBe(0);
  });

  it('should be able to clear all tasks', function () {
    // slim down capacity
    queue.capacity = 1;

    // assert inital emptiness
    expect(queue.status.queued).toBe(0);
    expect(queue.status.executing).toBe(0);

    // push past capacity
    queue.push(function(){});
    queue.push(function(){});

    expect(queue.status.queued).toBe(1);
    expect(queue.status.executing).toBe(1);

    // clear
    queue.clear();

    // assert executing and queued is empty
    expect(queue.status.queued).toBe(0);
    expect(queue.status.executing).toBe(0);
  });
});
